package com.kc.sjwh.action;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.kc.sjwh.entity.po.Zykzd;
import com.kc.sjwh.other.AjaxRes;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.other.QueryParam;
import com.kc.sjwh.service.ZykzdService;

/**
 * 资源库字典
 * @author zhouy
 *
 */
@Controller
@RequestMapping("zykzd")
public class ZykzdAction extends BaseAction{
	static Logger logger = Logger.getLogger(CzllyAction.class);
	
	@Autowired
	ZykzdService zykzdService;
	
	/**
	 * 打开列表
	 * @param req
	 * @return
	 */
	@RequestMapping("list")
	public String listPage(HttpServletRequest req){
		return "zykzd/zykzdList";
	}
	
	/**
	 * 进入编辑页面
	 * @param req
	 * @param id
	 * @return
	 */
	@RequestMapping("form")
	public String form(HttpServletRequest req, String id){
		req.setAttribute("act", id==null?"新增":"修改");
		Zykzd one = new Zykzd();
		if(null != id){
			one = zykzdService.getOneInfo(id);
		}
		req.setAttribute("obj", one);
		return "zykzd/zykzdForm";
	}
	
	/**
	 * 获得资源列表数据
	 * @param req
	 * @param pageSize
	 * @param pageNum
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getData")
	public String getData(HttpServletRequest req, int pageSize, int pageNum){
		QueryParam p = new QueryParam(pageSize, pageNum);
		p.addSqlCondition(" t.sybz = 'Y' ");
		String zyname = req.getParameter("zyname");
		if(null != zyname){
			zyname = zyname.trim();
			if(!"".equals(zyname)){
				p.addSqlCondition(" t.zysxmc like '%" + zyname + "%'");
			}
		}
		p.setOrder(" t.gxsj desc, t.bh asc");
		Page<Zykzd> page = zykzdService.getData(p);
		/*Table table = new Table();
		table.add(new Tr("属性代码", "属性名称", "资源类型", "创建时间", "更新时间", "操作"));//表头
		for(Zykzd one : page.getList()){
			table.add(new Tr(
					new Td(one.getZysx()),
					new Td(one.getZysxmc()),
					new Td(one.getZylxms()),
					new Td(new SimpleDateFormat("yyyy/MM/dd HH:mm").format(one.getCjsj())),
					new Td(new SimpleDateFormat("yyyy/MM/dd HH:mm").format(one.getGxsj())),
					new Td("<a href='form?id="+one.getBh()+"'>修改</a> &nbsp; "+ 
							"<a onclick=\"deleteData('"+one.getBh()+"')\">删除</a>")
			));
		}
		Map data = new HashMap();
		data.put("total", page.getTotal());
		data.put("html", table.toString());*/
		return JSON.toJSONString(new AjaxRes(page));
	}
	
	/**
	 * 删除数据
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delete")
	public String deleteData(HttpServletRequest req){
		Zykzd one = new Zykzd();
		one.setBh(req.getParameter("id"));
		one.setSybz("N");
		zykzdService.editData(one, "bh");
		return JSON.toJSONString(new AjaxRes());
	}
	
	/**
	 * 编辑资源
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping("save")
	public String save(HttpServletRequest req){
		AjaxRes ajaxRes  = new AjaxRes();
		String type = req.getParameter("czType");
		//判断属性代码是否存在
		QueryParam p = new QueryParam();
		String zysx = req.getParameter("zysx");
		String zylx = req.getParameter("zylx");
		p.addSqlCondition(" t.zysx = '" + zysx + "'");
		p.addSqlCondition(" t.zylx = '" + zylx + "'");
		if("edit".equals(type)){
			p.addSqlCondition(" t.bh <> '" + req.getParameter("bh") + "'");
		}
		List<Zykzd> list = zykzdService.getList(p);
		if(list.size() > 0){
			ajaxRes.setStatus("400");
			ajaxRes.setErrMsg("属性代码已存在!");
			ajaxRes.setMsg("属性代码已存在!");
		}else{
			Zykzd one = new Zykzd();
			if("add".equals(type)){
				String bh = UUID.randomUUID().toString().replace("-", "");
				one.setBh(bh);
				one.setZysx(req.getParameter("zysx"));
				one.setZysxmc(req.getParameter("zysxmc"));
				one.setZylx(req.getParameter("zylx"));
				one.setZylxms(req.getParameter("zylxms"));
				zykzdService.addData(one);
			}else{
				one.setBh(req.getParameter("bh"));
				one.setZysx(req.getParameter("zysx"));
				one.setZysxmc(req.getParameter("zysxmc"));
				one.setZylx(req.getParameter("zylx"));
				one.setZylxms(req.getParameter("zylxms"));
				one.setGxsj(new Date());
				zykzdService.editData(one, "bh");
			}
		}
		return JSON.toJSONString(ajaxRes);
	}
}
